(function ($app) {
    $app.module('gtPartyApp').controller('exam_democracyReviewPlanListCtrl', function ($scope, $http, $modal, $ocLazyLoad, $alert, $loading, $state, $bsRouterState, AuthUser, $timeout, $excel, $filter) {
        $state.params = $bsRouterState.$params($scope);
        $scope.isShow = false;
        $scope.moreTxt = "更多";
        $scope.loginUserId = AuthUser.getUser().Id;

        //定义数据集合
        $scope.dataList = []; //列表数据
        $scope.democracyReviewPlanStatusList = [];
        //查询条件
        $scope.selectparams = {
            pageindex: 1,
            pagesize: 10,
            year: null,
            title: '',
            startDate: null,
            endDate: null,
            recordStatus: null
        };
        $scope.resetparams = {};
        $scope.pageInfo = {ptotal: 0};
        angular.extend($scope.resetparams, $scope.selectparams);

        //定义模态框
        democracyReviewPlan_editCtrl.$inject = ['$scope', '$http', '$alert', '$loading'];
        democracyReviewPlan_editModal = $modal({
            resolve: {
                load: ['$ocLazyLoad', function ($ocLazyLoad) {
                }]
            },
            scope: $scope,
            controller: democracyReviewPlan_editCtrl,
            templateUrl: '../main/examine/democracyReview/plan/edit.html',
            show: false,
            animation: 'am-fade-and-slide-top'
        });

        democracyReviewPlan_detailCtrl.$inject = ['$scope', '$http', '$alert', '$loading'];
        democracyReviewPlan_detailModal = $modal({
            resolve: {
                load: ['$ocLazyLoad', function ($ocLazyLoad) {
                }]
            },
            scope: $scope,
            controller: democracyReviewPlan_detailCtrl,
            templateUrl: '../main/examine/democracyReview/plan/detail.html',
            show: false,
            animation: 'am-fade-and-slide-top'
        });

        //加载列表数据
        $scope.getList = function () {
            $http
            ({
                method: 'get', url: '../../api/examine/democracyReview/getPlanList', params: $scope.selectparams
            }).then(function (result) {
                $scope.dataList = result.data.item.list;
                $scope.dataList.forEach(it => {
                    it.rowChecked = false;
                });
                $scope.pageInfo.ptotal = result.data.item.total;
            }, function (res) {
                $scope.showMsg('错误', '服务器错误');
            });
        };

        $scope.search = function () {
            $scope.getList();
        };
        $scope.reset = function () {
            $scope.selectparams = {};
            angular.extend($scope.selectparams, $scope.resetparams);
            $scope.pageInfo = {ptotal: 0};
            $scope.getList();
        };
        $scope.edit = function (id) {
            democracyReviewPlan_editModal.params = {
                id: id,
                title: id ? "修改民主评议安排" : "新增民主评议安排"
            };
            democracyReviewPlan_editModal.parentLoad = $scope.getList;
            democracyReviewPlan_editModal.$promise.then(democracyReviewPlan_editModal.show);
        };

        $scope.detail = function (id) {
            democracyReviewPlan_detailModal.params = {
                id: id
            };
            democracyReviewPlan_detailModal.parentLoad = $scope.getList;
            democracyReviewPlan_detailModal.$promise.then(democracyReviewPlan_detailModal.show);
        };

        $scope.delete = function (id) {
            var ids = $.map($filter('filter')($scope.dataList, {rowChecked: true}), function (x) {
                return x.democracyReviewPlanId;
            });

            if (ids.length <= 0) {
                $scope.showMsg('提示', "请选择需要删除的数据");
                return false;
            }

            if ($scope.dataList.filter(it => it.rowChecked == true && it.recordStatus == 3).length > 0) {
                $scope.showMsg('提示', "请选择未考评的数据");
                return false;
            }

            if (confirm("确定删除？")) {
                $loading.show();
                $http({
                    method: "post",
                    url: "../../api/examine/democracyReview/delete",
                    data: {
                        ids: JSON.stringify(ids)
                    }
                }).then(function (result) {
                    $loading.hide();
                    if (result.data.success) {
                        $scope.showMsg('成功', "操作成功");
                        $scope.getList();
                    } else {
                        $scope.showMsg('失败', result.data.msg);
                    }
                }, function (resp) {
                    $loading.hide();
                    $scope.showMsg('错误', '服务器错误：' + resp.data);
                });
            }
        };

        $scope.submit = function (id) {
            var ids = $.map($filter('filter')($scope.dataList, {rowChecked: true}), function (x) {
                return x.democracyReviewPlanId;
            });

            if (ids.length <= 0) {
                $scope.showMsg('提示', "请选择需要提交的数据");
                return false;
            }

            if ($scope.dataList.filter(it => it.rowChecked == true && it.recordStatus != 1).length > 0) {
                $scope.showMsg('提示', "请选择未提交的数据");
                return false;
            }

            if (confirm("确定提交？")) {
                $loading.show();
                $http({
                    method: "post",
                    url: "../../api/examine/democracyReview/submit",
                    data: {
                        ids: JSON.stringify(ids)
                    }
                }).then(function (result) {
                    $loading.hide();
                    if (result.data.success) {
                        $scope.showMsg('成功', "操作成功");
                        $scope.getList();
                    } else {
                        $scope.showMsg('失败', result.data.msg);
                    }
                }, function (resp) {
                    $loading.hide();
                    $scope.showMsg('错误', '服务器错误：' + resp.data);
                });
            }
        };

        //获取字典数据
        $scope.getDemocracyReviewPlanStatusList = function () {
            $http({
                method: 'get',
                url: '../../api/dictionary/getDictionaryListByDicTypeKey',
                params: {
                    dicTypeKey: 'democracyReviewPlanStatus'
                }
            }).then(function (result) {
                $scope.democracyReviewPlanStatusList = result.data;
            }, function (resp) {

            });
        };

        $scope.$watch("selectparams.pageindex", function (newVal, oldVal) {
            if ($scope.pageInfo.ptotal > 0) {
                $scope.getList();
            }
        });

        $scope.$on('tabChildReloadData', function (event, data) {
            if (data.name == 'exam_democracyReviewPlanListCtrl') {
                $scope.getList();
            }
        });

        $scope.isShowClick = function () {
            $scope.isShow = !$scope.isShow;
            if ($scope.moreTxt == "更多") {
                $scope.moreTxt = "收起";
            } else if ($scope.moreTxt == "收起") {
                $scope.moreTxt = "更多";
            }
        };

        $scope.getList();
        $scope.getDemocracyReviewPlanStatusList();

    });
})(angular);